package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryIdMapper;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryIdStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private BrandCategoryIdMapper brandCategoryIdMapper;

    public BrandCategoryServiceImpl(){
        log.debug("创建业务对象: BrandCategoryServiceImpl");
    }

    @Override
    public void addNew(BrandCategoryAddNewDTO brandCategoryAddNewDTO) {
        log.debug("开始处理添加品牌类别业务,参数: {}", brandCategoryAddNewDTO);
        //从参数对象中获取品牌id
        Long brandId = brandCategoryAddNewDTO.getBrandId();
        //从参数对象中获取类别id
        Long categoryId = brandCategoryAddNewDTO.getCategoryId();
        //检查品牌和类别绑定关系是否已经被占用（品牌类别表中是否已经存在此绑定关系的数据）
        log.debug("检查品牌类别绑定关系是否已经被占用");
        int count = brandCategoryMapper.countByBrandIdAndCategoryId(brandId,categoryId);
        if(count>0){
            //是: 品牌和类别绑定关系已经被占用,添加品牌和类别绑定关系失败,抛出异常
            String message = "添加品牌和类别绑定关系失败，品牌和类别绑定关系已经被占用！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //否: 品牌和类别绑定关系没有被占用, 则向品牌类别表中插入数据
        log.debug("品牌和类别绑定关系没有被占用,将向品牌类别表中插入数据");
        BrandCategory brandCategory = new BrandCategory();
        BeanUtils.copyProperties(brandCategoryAddNewDTO,brandCategory);
        log.debug("即将插入品牌类别数据:{}",brandCategory);
        int rows = brandCategoryMapper.insert(brandCategory);
        if (rows != 1) {
            String message = "添加品牌类别失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入品牌类别数据完成");
    }

    @Override
    public void delete(Long brandId, Long categoryId) {
        log.debug("开始处理[根据品牌id和类别id删除品牌类别关系]的业务,参数: {},{}", brandId,categoryId);
        // 调用Mapper对象的getStandardById()执行查询
        BrandCategoryIdStandardVO queryResult = brandCategoryIdMapper.getStandardById(brandId,categoryId);
        if(queryResult == null){
            // 是：无此id对应的数据，将不允许执行删除操作，则抛出异常
            String message = "删除品牌类别失败,尝试访问的数据不存在! ";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除,参数:{}", queryResult.getId());
        int rows = brandCategoryIdMapper.deleteById(queryResult.getId());
        if (rows != 1) {
            String message = "添加类别失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }


}
